package com.jjsc.carousel.controller;

import com.jjsc.carousel.domain.Banner;
import com.jjsc.carousel.service.IBannerService;
import com.jjsc.common.core.web.controller.BaseController;
import com.jjsc.common.core.web.domain.AjaxResult;
import com.jjsc.common.core.web.page.TableDataInfo;
import com.jjsc.common.log.annotation.Log;
import com.jjsc.common.log.enums.BusinessType;
import com.jjsc.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @Author: 钟城明
 * @CreateTime: 2025-10-05
 * @Description:
 * @Version: 1.0
 */
@RestController
@RequestMapping("/banner")
public class BannerController extends BaseController {
    @Autowired
    private IBannerService bannerService;

    @GetMapping("/list")
    public TableDataInfo list( Banner banner) {
        startPage();
        List<Banner> list = bannerService.selectBannerList(banner);
        return getDataTable(list);
    }

    @GetMapping(value = "/{bannerId}")
    public AjaxResult getInfo(@PathVariable("bannerId") Long bannerId) {
        return AjaxResult.success(bannerService.getById(bannerId));
    }


    @Log(title = "轮播图管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Banner banner) {
        banner.setCreateBy(SecurityUtils.getUsername());
        return toAjax(bannerService.save(banner));
    }


    @Log(title = "轮播图管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Banner banner) {
        banner.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(bannerService.updateById(banner));
    }

    @Log(title = "轮播图管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{bannerIds}")
    public AjaxResult remove(@PathVariable Long[] bannerIds) {
        return toAjax(bannerService.removeByIds(Arrays.asList(bannerIds)));
    }


    @Log(title = "轮播图管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody Banner banner) {
        return toAjax(bannerService.updateById(banner));
    }
}
